package com.example.demo.common;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;

public class TestExportExcel {

	private static String EXPORT_XLSX_FILE_SUFFIX = ".xlsx";
	
	public static void main(String[] args) throws IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		String path = "D:\\testOrder2.xlsx";
		path = "D:\\testOrder2.xls";
		export(map, path);
		
	}
	
	public static void export(Map<String, Object> map ,String path) throws IOException {
		
		// 1、输出的文件地址及名称
		OutputStream out = new FileOutputStream(path);
		
		// 封装数据
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for(int i = 0; i < 100; i++) { //数据库为空,遍历了100000个
			Map<String, Object> temp_ = new HashMap<String, Object>();
			temp_.put("id", i + 1);
			temp_.put("number", i + 1);
			temp_.put("description", (i + 1) + "描述");
			list.add(temp_);
		}
		ExportExcel<List<Map<String, Object>>> exportExcel = new ExportExcel<List<Map<String, Object>>>();
		StringBuffer filename = new StringBuffer();
		filename.append("导出");
		filename.append(System.currentTimeMillis());
		if(StringUtils.isEmpty(map.get("excel_type"))) {
			filename.append(EXPORT_XLSX_FILE_SUFFIX);
		} else {
			filename.append(map.get("excel_type"));
		}

		// 3、生成格式是xlsx可存储103万行数据，如果是xls则只能存不到6万行数据
		// 判断Excel格式
		boolean end = path.endsWith("xlsx");

		if (end) {
			XSSFWorkbook workbook = new XSSFWorkbook();
			// 第一个表格内容
			exportExcel.exportXSExcelByColumn(workbook, 0, "测试1", new String[] {"id1", "number1", "description1"}, new String[] {"id", "number", "description"}, list, out, null);
			// 第二个表格内容
			exportExcel.exportXSExcelByColumn(workbook, 1, "测试2", new String[] {"id2", "number2", "description2"}, new String[] {"id", "number", "description"}, list, out, null);
			// 将所有的数据一起写入，然后再关闭输入流。
			workbook.write(out);
		} else {
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 第一个表格内容
			exportExcel.exportHSExcelByColumn(workbook, 0, "测试1", new String[] {"id", "number", "description"}, new String[] {"id", "number", "description"}, list, out, null);
			// 第二个表格内容
			exportExcel.exportHSExcelByColumn(workbook, 1, "测试2", new String[] {"id2", "number2", "description2"}, new String[] {"id", "number", "description"}, list, out, null);
			// 将所有的数据一起写入，然后再关闭输入流。
			workbook.write(out);
		}

		out.close();
	}

	public void export(Map<String, Object> map ,HttpServletResponse response) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for(int i = 0; i < 100; i++) { //数据库为空,遍历了100000个
			Map<String, Object> temp_ = new HashMap<String, Object>();
			temp_.put("id", i + 1);
			temp_.put("number", i + 1);
			temp_.put("description", (i + 1) + "描述");
			list.add(temp_);
		}
		ExportExcel<List<Map<String, Object>>> exportExcel = new ExportExcel<List<Map<String, Object>>>();
		StringBuffer filename = new StringBuffer();
		filename.append("导出");
		filename.append(System.currentTimeMillis());
		if(StringUtils.isEmpty(map.get("excel_type"))) {
			filename.append(EXPORT_XLSX_FILE_SUFFIX);
		} else {
			filename.append(map.get("excel_type"));
		}
		OutputStream out = null;
		try {
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + new String(filename.toString().getBytes("UTF-8"), "ISO8859-1"));
			out = response.getOutputStream();
			exportExcel.exportXSExcelByColumn("Title", new String[] {"id", "number", "description"}, new String[] {"id", "number", "description"}, 
					list, out ,null);
		} catch (IOException e) {		
		}
	}

}
